package com.admin;


import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;


import com.Constants;




public class CompanyService extends RoleService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3608339302116586551L;
	protected  String table="\"Company\"";
	protected List<Company> companys;
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	protected  Company selectCompany(Map pharm) {
		Company com=new Company(); 
		String where="\"Checked\"=1";
		Iterator<String> iter = pharm.keySet().iterator();
		while(iter.hasNext()){
			   String key=iter.next();
			   String value = (String) pharm.get(key);
			   where+=" and \""+key+"\"='"+value+"'";
		}
		Map map=new HashMap();
		map.put("table", table);
		map.put("where", where);
		com=selectcombysql(map);
		return com;
	}
	
	@SuppressWarnings({ "rawtypes" })
	protected   Company selectcombysql(Map map) {
		SqlSession sqlSession = getSqlSession();
		Company com=new Company(); 
		try {
		CompanyMapper commp=sqlSession.getMapper(CompanyMapper.class);
		com=commp.selectcombysql(map);
		} finally {
			sqlSession.close();
		}
		return com;
	}
	
	@SuppressWarnings("unchecked")
	protected   List<Company> selectcomsbysql(@SuppressWarnings("rawtypes") Map map) {
		SqlSession sqlSession = getSqlSession();
		 List<Company> com; 
		try {
	    map.put("table", table);
		CompanyMapper commp=sqlSession.getMapper(CompanyMapper.class);
		com=commp.selectcomsbysql(map);
		} finally {
			sqlSession.close();
		}
		return com;
	}
	
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected  List<Company> getcome(){
		Map map=new HashMap();
		map.put("table", table);
		map.put("MinComid", 0);
		map.put("limit", 10000);
		List<Company> com=selectcomlistbysql(map);
		return com;
	}
	
	@SuppressWarnings({ "rawtypes", "unchecked" })//新增方法
	protected  List<LinkedHashMap<String, String>> getcomes(Map params){
		String where="1=1";
		String fy="";
		Iterator<String> iter = params.keySet().iterator();
		while(iter.hasNext()){
			   String key=iter.next();
			   String value = (String) params.get(key);
             if(key.equals("between")) {
				   
            	   fy+=" and "+value;
			   }else  if(key.equals("like")) {
				   
            	 where+=" and "+value;
			   }
             else  if(!key.equals("nr")||!key.equals("table")||!key.equals("px")){
				   
				   where+=" and \""+key+"\"='"+value+"'";
			   }
		 }
		
		Map map=new HashMap();
		map.put("table", table);
		map.put("px", "\"Comid\" desc");
		map.put("where", where);
		map.put("fy", fy);
		map.putAll(params);
		List<LinkedHashMap<String, String>> com=selecttable(map);
		return com;
	}

	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	protected String getsfyxty(Map params) {
		SqlSession sqlSession = getSqlSession();
		try {
			CompanyMapper commp=sqlSession.getMapper(CompanyMapper.class);
			params.put("table", table);
			List<Company> com=commp.sfyxty(params);
			if(com.size()==0) {
				
				message="0";
			}else {
				
				message="1";
			}
		}finally {
			sqlSession.close();
	 }
		return message;
	}
	

	@SuppressWarnings("rawtypes")
	protected  List<Company> selectcomlistbysql(Map map) {
		SqlSession sqlSession = getSqlSession();
		List<Company> com;
		try {
			CompanyMapper commp=sqlSession.getMapper(CompanyMapper.class);
			com=commp.selectcomlistbysql(map);
		}finally {
					sqlSession.close();
			 }
				return com;
	}
	
	
	
		
		
			//新增公司
			protected  int newcompany(Company company) {
				int result=0;
				User user = (User)session.get(Constants.USER);
				SqlSession sqlSession = getSqlSession();
				try {
					CompanyMapper comMapper = sqlSession.getMapper(CompanyMapper.class);
					List<Company>	coms=selectcompanybycomname(company.getComname());
					if (!isListEmpty(coms)) {
						//message = "新增公司失败！失败原因：公司名称已存在！";
						result=-1;
						return result;
					}
					company.setCreateuserid(user.getUserid());
					company.setXguserid(user.getUserid());
		
					result = comMapper.insertCompany(company);
				} finally {
					sqlSession.close();
				}
				return result;
			}

			//查询公司通过公司名字
			protected List<Company> selectcompanybycomname(String comname) {
				SqlSession sqlSession = getSqlSession();
				try {
					CompanyMapper comMapper = sqlSession.getMapper(CompanyMapper.class);
					companys = comMapper.selectCompanyByName(comname);
				} finally {
					sqlSession.close();
				}
				return  companys;
			}
					
			//删除公司
			protected int delectcompany(String comids){//删除进行角色判断
				SqlSession sqlSession = getSqlSession();
				CompanyMapper mapper =sqlSession.getMapper(CompanyMapper.class);
				int result =1;
				try{
					String[] comidss=comids.split(",");//将传入的字符串分割成数组
					for(int i=0;i<comidss.length;i++){
						int comid =Integer.parseInt(comidss[i]);
						result*=mapper.delectCompanyByComid(comid);
						if(result == 0)  break;
					}
					if(result !=0){
						sqlSession.commit();//删除成功提交数据库操作
					}else{
						sqlSession.rollback();//删除失败不提交
					}
					
				}finally{
					sqlSession.close();
				}
				return result;
			}
			
			//修改公司
			@SuppressWarnings({ "rawtypes", "unchecked" })
			protected int companymod(Map pharm){
				/*int result =0;
				SqlSession sqlSession = getSqlSession();
				try{
					CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
					result=mapper.updateConpany(company);
				}finally{
					sqlSession.close();
				}*/
				pharm.put("table", table);
				int result=updatetablebykey(pharm);
				return result;
			}
			
			//停启公司
			protected int xgcompanystatus(Company company){
				int result;
				SqlSession sqlSession = getSqlSession();
				try{
					CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
					result = mapper.updateConpanystatus(company);
				}finally{
					sqlSession.close();
				}
				return result;
			}
			
		
			
			
			//
			protected String selectComidByComname(String comname){
				String result;
				SqlSession sqlSession = getSqlSession();
				try {
					CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
					result = mapper.selectComidByComname(comname);
				} finally {
					sqlSession.close();
				}
				return result;
			}
			
			//3.通过Comid在Company表中提取出公司名并返回
			@SuppressWarnings("rawtypes")
			protected List<Company> selectComnameByComid(Map map){
				SqlSession sqlSession = getSqlSession();
				try {
					CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
					companys = mapper.selectComnameByComid(map);
				} finally {
					sqlSession.close();
				}
				return companys;
			}
			
			
			//通过公司下拉框中选中的公司名称提取出角色名
			@SuppressWarnings("rawtypes")
			protected List<Role> selectRolenameByComname(Map map){
				SqlSession  sqlSession = getSqlSession();
				List<Role> roles ;
				try {
					RoleMapper mapper = sqlSession.getMapper(RoleMapper.class);
					roles = mapper.selectRolenameByComname(map);
				} finally {
					sqlSession.close();
				}
				return roles;
			}
			
			//切换公司-关闭旧公司
			@SuppressWarnings("rawtypes")
			protected int updateoldCompany(Map map){
				int result;
				SqlSession sqlSession = getSqlSession();
				try {
					CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
					result = mapper.updateoldCompany(map);
				} finally {
					sqlSession.close();
				}
				return result;
			}
			
			//切换公司-开启新公司
			@SuppressWarnings("rawtypes")
			protected int updateCompanyByComname(Map map){
				int result;
				SqlSession sqlSession = getSqlSession();
				try {
					CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
					result = mapper.updateCompanyByComname(map);
				} finally {
					sqlSession.close();
				}
				return result;
			}
			
			@SuppressWarnings("rawtypes")
			protected List<Company> selectcomnames(Map map){
				List<Company> list;
				SqlSession sqlSession = getSqlSession();
				try {
					CompanyMapper mapper = sqlSession.getMapper(CompanyMapper.class);
					list = mapper.selectcomnames(map);
				} finally {
					sqlSession.close();
				}
				return list;
			}
}
